package com.x_memteor.base_master.utils.converter.aesConverterFactory;

import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
import com.google.gson.reflect.TypeToken;
import com.x_memteor.base_master.utils.converter.jsonConverterFactory.JsonConverterFactory;
import com.x_memteor.base_master.utils.converter.jsonConverterFactory.JsonRequestBodyConverter;
import com.x_memteor.base_master.utils.converter.jsonConverterFactory.JsonResponseBodyConverter;

import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Converter;
import retrofit2.Retrofit;

/**
 * @author: X_Meteor
 * @description: 类描述
 * @version: V_1.0.0
 * @date: 2018/2/28 0028 下午 8:43
 * @company:
 * @email: lx802315@163.com
 */
public class AesConverterFactory extends Converter.Factory {
    public static AesConverterFactory create() {
        return create(new Gson());
    }

    public static AesConverterFactory create(Gson gson) {
        return new AesConverterFactory(gson);
    }

    private final Gson gson;

    private AesConverterFactory(Gson gson) {
        if (gson == null) throw new NullPointerException("gson == null");
        this.gson = gson;
    }


    @Override
    public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {

        TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(type));
        return new AesResponseBodyConverter<>(gson, adapter); //响应
    }

    @Override
    public Converter<?, RequestBody> requestBodyConverter(Type type, Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {

        TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(type));
        return new AesRequestBodyConverter<>(gson, adapter); //请求
    }
}
